package com.example.lenovo.a203_yhy_ordering_3.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.a203_yhy_ordering_3.R;
import com.example.lenovo.a203_yhy_ordering_3.activity.LoginActivity;
import com.example.lenovo.a203_yhy_ordering_3.activity.RegisterActivity;
import com.example.lenovo.a203_yhy_ordering_3.activity.UpdateUserActivity;
import com.example.lenovo.a203_yhy_ordering_3.beans.UserInfoBean;
import com.example.lenovo.a203_yhy_ordering_3.listener.UserInfoListener;
import com.example.lenovo.a203_yhy_ordering_3.model.UserInfoModel;
import com.squareup.picasso.Picasso;

public class MeFragment extends Fragment {
    private ImageView im_touxiang,im_xiugai;
    private TextView te_name,te_phone,te_liuyan;
    private int user_id;
    private UserInfoBean userInfoBeans;
    private SharedPreferences sharedPreferences;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.activity_me_fragment,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initRecycleView(view);
        sharedPreferences=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        user_id=Integer.parseInt(sharedPreferences.getString("userid","获取userid失败"));

        UserInfoModel userInfoModel = new UserInfoModel();
        userInfoModel.getUserInfoResult(user_id,userInfoListener);

        initEvents();
    }

    public void initRecycleView(View view){
        te_name=view.findViewById(R.id.textView14);
        te_phone=view.findViewById(R.id.textView15);
        te_liuyan=view.findViewById(R.id.textView16);
        im_touxiang=view.findViewById(R.id.imageView4);
        im_xiugai=view.findViewById(R.id.imageView9);
    }



    UserInfoListener userInfoListener = new UserInfoListener() {
        @Override
        public void onResponse(UserInfoBean userInfoBean) {
            userInfoBeans=userInfoBean;
            te_name.setText(userInfoBean.getUsername());
            te_phone.setText(userInfoBean.getMobilenum());
            te_liuyan.setText(userInfoBean.getAddress());
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
        }
    };

    public void initEvents(){
        im_xiugai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(),UpdateUserActivity.class);
                startActivity(intent);
            }
        });
    }
}
